#include "mainwindow.h"
#include "ui_mainwindow.h"
#include <QtGui>
#include <QDebug>

MainWindow::MainWindow(QWidget *parent) :
    QMainWindow(parent),
    ui(new Ui::MainWindow)
{
    ui->setupUi(this);

    mdiArea = new QMdiArea;
    mdiArea->setHorizontalScrollBarPolicy(Qt::ScrollBarAsNeeded);
    mdiArea->setVerticalScrollBarPolicy(Qt::ScrollBarAsNeeded);
    setCentralWidget(mdiArea);

    createMenu();
    loadPlugins();
    createPlugins();
    /*connectAction();


    for(int i = 0; i < cusMenuList.size(); i++)
    {
        if(cusMenuList.at(i)->title() == "&File" )
        {
            cusMenuList.at(i)->addAction(cusAction);
            //break;
        }
    }
    cusWidgetCpy = NULL;
    subWindow = NULL;*/
}

MainWindow::~MainWindow()
{
    delete ui;
}

void MainWindow::loadPlugins()
{
    QDir pluginsDir(qApp->applicationDirPath());

#if defined(Q_OS_WIN)
    if (pluginsDir.dirName().toLower() == "debug" || pluginsDir.dirName().toLower() == "release")
        pluginsDir.cdUp();
#elif defined(Q_OS_MAC)
    if (pluginsDir.dirName() == "MacOS") {
        pluginsDir.cdUp();
        pluginsDir.cdUp();
        pluginsDir.cdUp();
    }
#endif
    pluginsDir.cd("plugins");

    foreach (QString fileName, pluginsDir.entryList(QDir::Files)) {
        QPluginLoader *pluginLoader =
                new QPluginLoader(pluginsDir.absoluteFilePath(fileName));

        QObject *plugin = pluginLoader->instance();
        if (plugin)
        {
            pluginLoaderList.push_back(pluginLoader);
        }
    }

    qDebug() << "Plugins num: " << pluginLoaderList.size();
}

void MainWindow::createPlugins()
{
    for( int i = 0; i < pluginLoaderList.size(); i++ )
    {
        QObject *plugin = pluginLoaderList[i]->instance();
        if (plugin)
        {/*
            WidgetPluginInterface* interface = qobject_cast<WidgetPluginInterface *>(plugin);
            if( interface )
            {
                QString cusMenuName = interface->getCustomActionPos();
                QString cusActionName = interface->getCustomActionName();
                if( !cusAction )
                {
                    cusAction = new QAction(cusActionName,this);
                }
                cusWidget = interface->getWidget();
            }*/
            SignalSlotPluginInterface* interface = qobject_cast<SignalSlotPluginInterface *>(plugin);
            if( interface )
            {
                qDebug() << "SignalSlotPluginInterface successed!";
                QString cusActionName = interface->getCustomActionName();
                QAction* iAction = new QAction(cusActionName,this);
                cusMenuList[0]->addAction(iAction);
                connect(iAction,SIGNAL(triggered()),interface,SLOT(alertsSlot()));
                connect(interface,SIGNAL(alert(QString)),this,SLOT(takeMessage(QString)));

            }

            //connect(iAction,SIGNAL(triggered()),interface,SLOT(alertsSlot()));


        }
    }
}

void MainWindow::takeMessage(QString message)
{
    qDebug() << "Message taken: " << message;
}

QWidget* MainWindow::createWidgetPlugin(QPluginLoader *loader)
{
    QObject *plugin = loader->instance();
    if(plugin)
    {
        PluginInterface* interface = qobject_cast<WidgetPluginInterface *>(plugin);
        QString cusMenuName = interface->getCustomActionPos();
        QString cusActionName = interface->getCustomActionName();
        cusAction = new QAction(cusActionName,this);
        cusWidget = interface->getWidget();
        return cusWidget;
    }
    return 0;
}

void MainWindow::showCusWidget()
{
/*    QDir pluginsDir(qApp->applicationDirPath());

#if defined(Q_OS_WIN)
    if (pluginsDir.dirName().toLower() == "debug" || pluginsDir.dirName().toLower() == "release")
        pluginsDir.cdUp();
#elif defined(Q_OS_MAC)
    if (pluginsDir.dirName() == "MacOS") {
        pluginsDir.cdUp();
        pluginsDir.cdUp();
        pluginsDir.cdUp();
    }
#endif
    pluginsDir.cd("plugins");

    foreach (QString fileName, pluginsDir.entryList(QDir::Files)) {
        QPluginLoader pluginLoader(pluginsDir.absoluteFilePath(fileName));
        QObject *plugin = pluginLoader.instance();
        if (plugin) {
            PluginInterface* interface = qobject_cast<WidgetPluginInterface *>(plugin);
            if (interface)
            {
                cusWidget = interface->getWidget();
                mdiArea->addSubWindow(cusWidget);
                cusWidget->show();
            }
        }
    }
    */
   // mdiArea->addSubWindow(cusWidget);
   // cusWidget->show();
        //cusWidgetCpy = new QWidget(cusWidget);
        createPlugins();
        subWindow = new QMdiSubWindow;
        subWindow->setWidget(cusWidget);
        subWindow->setAttribute(Qt::WA_DeleteOnClose);
        mdiArea->addSubWindow(subWindow);
        subWindow->show();
        disconnectAction(cusAction);
        //connect(subWindow,SIGNAL(destroyed()),this,SLOT(connectAction()));
        return;

        //disconnectAction();
       // connect(subWindow,SIGNAL(destroyed()),this,SLOT(connectAction()));
}

void MainWindow::connectAction()
{
    connect(cusAction, SIGNAL(triggered()),this,SLOT(showCusWidget()));
}

void MainWindow::disconnectAction(QAction* act)
{
    disconnect(act, SIGNAL(triggered()),this,SLOT(showCusWidget()));
    connect(subWindow,SIGNAL(destroyed()),this,SLOT(connectAction()));
}

void MainWindow::echoTest()
{
    qDebug() << "Test";
}

void MainWindow::createMenu()
{
    fileMenu = menuBar()->addMenu(tr("&File"));
    qDebug() << fileMenu->title();
    cusMenuList.push_back(fileMenu);

   // QMenu* cusMenu = new QMenu();
   // cusMenuList.push_back(cusMenu);
   // cusMenu = menuBar()->addMenu(tr("Custom Menu"));
}

void MainWindow::addCusMenu(QString menuName)
{
    if( !menuName.isNull() )
    {
        QMenu* cusMenu = new QMenu(menuName);
        cusMenuList.push_back(cusMenu);
        menuBar()->addMenu(cusMenu);
    }
}
